package com.oasistudio.tk.connector.imp;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TreeMap;

import org.json.JSONArray;
import org.json.JSONObject;

import com.oasistudio.tk.connector.TopAPIConnectorHelper;
import com.oasistudio.tk.connector.TopApiRequest;
import com.oasistudio.tk.connector.TopApiResponse;
import com.taobao.tbw.common.TBWContext;

/**
 * 获取淘宝画报的所有频道
 */
public class TaobaoPosterDetailGetConnector implements TopAPIConnectorHelper {
	private String posterId;
	public TaobaoPosterDetailGetConnector() {
	}
	
	public void setPosterId(String posterId){
		this.posterId = posterId;
	}

	@Override
	public Object asyncPaser(byte[] part) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getApiUrl() {
		TopApiRequest request = new TopApiRequest();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		TreeMap<String, String> params = new TreeMap<String, String>();
		params.put("method", "taobao.poster.posterdetail.get");
		params.put("session", TBWContext.getInstance().getTopsession());
		params.put("timestamp", sdf.format(calendar.getTime()));
		params.put("format", "json");
		params.put("app_key", TBWContext.getInstance().getAppKey());
		params.put("v", "2.0");
		params.put("partner_id", "top-apitools");
		params.put("sign_method", "md5");
		//API method 参数
		params.put("poster_id", posterId);
		request.params = params;
		String url = request.generalRequestUrl(true);
		System.out.println("TaobaoPosterDetailGetConnector.getApiUrl() url = "+url);
		return url;
	}

	/**
	 * 
	 */
	@Override
	public Object syncPaser(byte[] all) {
		PosterDetailInfo posterDetailInfo = new PosterDetailInfo();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			TopApiResponse response = new TopApiResponse("poster_posterdetail_get_response");
			String str = new String(all, "UTF-8");
			//Log.d("return msg:", str);
			if (response.parseResult(str).success) {
				posterDetailInfo.isSuccess = true;
				JSONObject jsObj = response.data;
				if (jsObj.has("poster_pics")) {
					JSONObject itemsJSON = jsObj.getJSONObject("poster_pics");
					if (itemsJSON.has("huabao_picture")) {
						JSONArray postersArray = itemsJSON.getJSONArray("huabao_picture");
						PosterPicture[] posterPictures = new PosterPicture[postersArray.length()];
						posterDetailInfo.posterPictures = posterPictures;
						for (int i = 0; i < postersArray.length(); i++) {
							JSONObject posterJSON = postersArray.getJSONObject(i);
							posterPictures[i] = new PosterPicture();
							posterPictures[i].pic_id = posterJSON.optLong("pic_id");
							posterPictures[i].create_date = sdf.parse(posterJSON.optString("create_date"));
							posterPictures[i].modified_date = sdf.parse(posterJSON.optString("modified_date"));
							posterPictures[i].pic_note = posterJSON.optString("pic_note");
							posterPictures[i].poster_id = posterJSON.optInt("poster_id");
							posterPictures[i].pic_url = posterJSON.optString("pic_url");
						}
					}
				}
			} else {
				posterDetailInfo.isSuccess = false;
				posterDetailInfo.errCode = response.errCode;
				posterDetailInfo.errInfo = response.errInfo;
			}

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return posterDetailInfo;
	}

	public static class PosterDetailInfo {
		public String errCode;
		public String errInfo;
		public Boolean isSuccess;
		public PosterPicture[] posterPictures;
	}

	public static class PosterPicture {
		public Date create_date;//
		public Date modified_date;//
		public String pic_note;
		public String pic_url;
		public Number pic_id;
		public Number poster_id;
		
		public String toString(){
			StringBuilder sb = new StringBuilder();
			sb.append("pic_id:"+pic_id);
			sb.append("poster_id:"+poster_id);
			sb.append(";pic_note:"+pic_note);
			sb.append(";create_date:"+create_date);
			sb.append(";modified_date:"+modified_date);
			return sb.toString();
		}
	}
}
